<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib uri="http://www.springframework.org/security/tags" prefix="sec"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Search</title>
</head>
<body>

	<form:form action="${pageContext.request.contextPath}/search" method="POST" commandName="searchAdvertDto">
		<table>
			<tr>
				<th>Attribute</th>
				<th>Search value</th>
				<th>Search type</th>
				<th>Search condition</th>
			</tr>
			<tr>
				<td>Title</td>
				<td><form:input type="text" name="title" path="title"/></td>
				<sec:authorize access="isAuthenticated()">
					<td><form:select path="titleSearchType" items="${ searchTypes }"></form:select></td>
				</sec:authorize>
				<sec:authorize access="isAnonymous()">
					<td>
						<form:select path="titleSearchType">
							<c:forEach var="currentSearchType" items="${ searchTypes }">
								<c:if test="${ currentSearchType eq 'Regular' }">
									<form:option value="${ currentSearchType }">
										<c:out value="${ currentSearchType }"></c:out>
									</form:option>
								</c:if>
							</c:forEach>
						</form:select>
					</td>
				</sec:authorize>
				<td><form:select path="titleOccur" items="${ occures }"></form:select></td>
			</tr>
			<tr>
				<td>Advertiser</td>
				<td>
					<form:select path="advertiser" multiple="false">
						<c:choose>
							<c:when test="${ empty advertisers }">
								<form:option value="-1">-- No advertisers available --</form:option>
							</c:when>
							<c:otherwise>
								<form:option value="-1">-- Select advertiser --</form:option>
								<c:forEach items="${ advertisers }" var="currentAdvertiser">
									<form:option value="${ currentAdvertiser.id }">
										<c:out value="${ currentAdvertiser.username }"></c:out>
									</form:option>
								</c:forEach>
							</c:otherwise>
						</c:choose>
					</form:select>
				</td>
				<sec:authorize access="isAuthenticated()">
					<td><form:select path="advertiserSearchType" items="${ searchTypes }"></form:select></td>
				</sec:authorize>
				<sec:authorize access="isAnonymous()">
					<td>
						<form:select path="advertiserSearchType">
							<c:forEach var="currentSearchType" items="${ searchTypes }">
								<c:if test="${ currentSearchType eq 'Regular' }">
									<form:option value="${ currentSearchType }">
										<c:out value="${ currentSearchType }"></c:out>
									</form:option>
								</c:if>
							</c:forEach>
						</form:select>
					</td>
				</sec:authorize>
				<td><form:select path="advertiserOccur" items="${ occures }"></form:select></td>
			</tr>
			<tr>
				<td>Keywords</td>
				<td><form:input path="keyWords" type="text" /></td>
				<sec:authorize access="isAuthenticated()">
					<td><form:select path="keyWordsSearchType" items="${ searchTypes }"></form:select></td>
				</sec:authorize>
				<sec:authorize access="isAnonymous()">
					<td>
						<form:select path="keyWordsSearchType">
							<c:forEach var="currentSearchType" items="${ searchTypes }">
								<c:if test="${ currentSearchType eq 'Regular' }">
									<form:option value="${ currentSearchType }">
										<c:out value="${ currentSearchType }"></c:out>
									</form:option>
								</c:if>
							</c:forEach>
						</form:select>
					</td>
				</sec:authorize>
				<td><form:select path="keyWordsOccur" items="${ occures }"></form:select></td>
			</tr>
			<tr>
				<td>Abstract text</td>
				<td><form:input path="abstractText" type="text" /></td>
				<sec:authorize access="isAuthenticated()">
					<td><form:select path="abstractTextSearchType" items="${ searchTypes }"></form:select></td>
				</sec:authorize>
				<sec:authorize access="isAnonymous()">
					<td>
						<form:select path="abstractTextSearchType">
							<c:forEach var="currentSearchType" items="${ searchTypes }">
								<c:if test="${ currentSearchType eq 'Regular' }">
									<form:option value="${ currentSearchType }">
										<c:out value="${ currentSearchType }"></c:out>
									</form:option>
								</c:if>
							</c:forEach>
						</form:select>
					</td>
				</sec:authorize>
				<td><form:select path="abstractTextOccur" items="${ occures }"></form:select></td>
			</tr>
			<tr>
				<td>Advert text</td>
				<td><form:input path="advertContent" type="text" /></td>
				<sec:authorize access="isAuthenticated()">
					<td><form:select path="advertContentSearchType" items="${ searchTypes }"></form:select></td>
				</sec:authorize>
				<sec:authorize access="isAnonymous()">
					<td>
						<form:select path="advertContentSearchType">
							<c:forEach var="currentSearchType" items="${ searchTypes }">
								<c:if test="${ currentSearchType eq 'Regular' }">
									<form:option value="${ currentSearchType }">
										<c:out value="${ currentSearchType }"></c:out>
									</form:option>
								</c:if>
							</c:forEach>
						</form:select>
					</td>
				</sec:authorize>
				<td><form:select path="advertContentOccur" items="${ occures }"></form:select></td>
			</tr>
			<tr>
				<td>Category</td>
				<td>
					<form:select path="category" multiple="false">
						<c:choose>
							<c:when test="${ empty categories }">
								<form:option value="-1">-- No categories available --</form:option>
							</c:when>
							<c:otherwise>
								<form:option value="-1">-- Select category --</form:option>
								<c:forEach items="${ categories }" var="currentCategory">
									<form:option value="${ currentCategory.id }">
										<c:out value="${ currentCategory.name }"></c:out>
									</form:option>
								</c:forEach>
							</c:otherwise>
						</c:choose>
					</form:select>
				</td>
				<sec:authorize access="isAuthenticated()">
					<td><form:select path="categorySearchType" items="${ searchTypes }"></form:select></td>
				</sec:authorize>
				<sec:authorize access="isAnonymous()">
					<td>
						<form:select path="categorySearchType">
							<c:forEach var="currentSearchType" items="${ searchTypes }">
								<c:if test="${ currentSearchType eq 'Regular' }">
									<form:option value="${ currentSearchType }">
										<c:out value="${ currentSearchType }"></c:out>
									</form:option>
								</c:if>
							</c:forEach>
						</form:select>
					</td>
				</sec:authorize>
				<td><form:select path="categoryOccur" items="${ occures }"></form:select></td>
			</tr>
			<tr>
				<td colspan="2" align="center"><input type="reset" value="Reset"/></td>
				<td colspan="2" align="center"><input type="submit" value="Search"/></td>
			</tr>
		</table>
	</form:form>

	<br />

	<sec:authorize access="hasAnyRole('${ advertiserRole }', '${ adminRole }')">
		Since you are logged as advertiser, you are allowed to search only your adverts.
		<br>
		Log out and Log in or register as agent to view all results.
		
		<br><br>
		Search CVs
		<form:form action="${pageContext.request.contextPath}/search/searchAgents" method="POST" commandName="searchAgentCvDto">
			<table>
				<tr>
					<td>CV text</td>
					<td><form:input path="cvText" type="text" /></td>
					<td><form:select path="searchType" items="${ searchTypes }"></form:select></td>
					<td><form:select path="occur" items="${ occures }"></form:select></td>
					<td><form:errors path="cvText"></form:errors></td>
				</tr>
				<tr>
					<td colspan="2" align="center"><input type="reset" value="Reset"/></td>
					<td colspan="2" align="center"><input type="submit" value="Search"/></td>
				</tr>
			</table>
		</form:form>
	</sec:authorize>

</body>
</html>